﻿@model List<XukaShop.Models.Entities.Order>

@{
    ViewBag.Title = "Index";
    Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}

@section Breadcrumbs
{
    <div class="media">
        <div class="pageicon pull-left">
            <i class="fa fa-home"></i>
        </div>
        <div class="media-body">
            <ul class="breadcrumb">
                <li><a href="@Url.Action("Index", "Dashboard")"><i class="glyphicon glyphicon-home"></i></a></li>
                <li><a href="@Url.Action("Index", "Dashboard")">Trang chủ</a></li>
            </ul>
            <h4>Danh sách đơn hàng</h4>
        </div>
    </div>
}

@section MenuBar
{
    <h5 class="leftpanel-title">Menu</h5>
    <ul class="nav nav-pills nav-stacked">
        <li><a href="@Url.Action("Index", "Dashboard")"><i class="fa fa-home"></i><span>Trang chủ</span></a></li>
        <li class="active"><a href="@Url.Action("Index", "Order")"><i class="glyphicon glyphicon-shopping-cart"></i><span>Đặt hàng</span></a></li>
        <li><a href="@Url.Action("Index", "Message")"><i class="glyphicon glyphicon-envelope"></i><span>Tin nhắn</span></a></li>
        <li class="parent"><a href="#"><i class="fa fa-file-text"></i><span>Sản phẩm</span></a>
            <ul class="children">
                <li><a href="@Url.Action("ProductList", "Product")">Tất cả sản phẩm</a></li>
                <li><a href="@Url.Action("NewProduct", "Product")">Sản phẩm mới</a></li>
                <li><a href="@Url.Action("CommingSoonProduct", "Product")">Sản phẩm sắp có</a></li>
                <li><a href="@Url.Action("Index", "Brand")">Nhãn hàng</a></li>
            </ul>
        </li>
        <li><a href="@Url.Action("Index", "Promotion")"><i class="glyphicon glyphicon-bullhorn"></i><span>Giảm giá</span></a></li>
        <li><a href="@Url.Action("MemberList", "Member")"><i class="glyphicon glyphicon-user"></i><span>Thành viên</span></a></li>
        <li class="parent"><a href="#"><i class="fa fa-file-text"></i><span>Quản lý chung</span></a>
            <ul class="children">
                <li><a href="@Url.Action("Index", "Banner")">Banners</a></li>
            </ul>
        </li>
    </ul>
}

<div class="panel panel-default-head">
    <div class="panel-heading">
        <h4 class="panel-title">Danh sách đơn hàng</h4>        
    </div>
    <!-- panel-heading -->

    <table id="orderTable" class="table table-striped table-bordered responsive">
        <thead class="">
            <tr>
                <th>Mã đặt hàng</th>
                <th>Ngày đặt</th>
                <th>Tổng giá tiền</th>
                <th>Giảm giá</th>
                <th>Tình trạng</th>
                <th style="text-align: center">#</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var order in Model)
            {
                <tr>
                    <td>
                        @order.OrderCode
                    </td>
                    <td>
                        @order.DateCreate.ToString("dd/MM/yyyy")
                    </td>
                    <td>
                        @order.TotalSalePrice.ToString("#,###")
                    </td>
                    <td>
                        @order.IsDiscount
                    </td>
                    <td>
                        @order.OrderStatus.StatusName
                    </td>
                    <td class="table-action">
                        <a href="@Url.Action("OrderDetail", "Order", new { orderCode = order.OrderCode })" data-toggle="tooltip" title="" class="tooltips" data-original-title="Sửa"><i class="fa fa-pencil"></i></a>
                        <a href="#" data-toggle="tooltip" title="" class="delete-row tooltips" id="delete_order_@order.OrderCode" data-original-title="Xóa"><i class="fa fa-trash-o" onclick="deletePromotion('@order.OrderCode')"></i></a>
                    </td>
                </tr>
            }
        </tbody>
    </table>
</div>

<script>
    jQuery(document).ready(function () {
        jQuery('#orderTable').DataTable({
            responsive: true
        });
    });

    function showMessageDeleteSuccess() {
        jQuery.gritter.add({
            title: 'Thành công!',
            text: 'Xóa đơn đặt hàng thành công!',
            class_name: 'growl-success',
            sticky: false,
            time: '1000'
        });
        return false;
    }

    function showMessageDeleteFail() {
        jQuery.gritter.add({
            title: 'Thất bại!',
            text: 'Xóa đơn đặt hàng thất bại!',
            class_name: 'growl-danger',
            sticky: false,
            time: '1000'
        });
        return false;
    }

    function deletePromotion(orderCode) {
        bootbox.confirm("Bạn có muốn xóa đơn đặt hàng này không?", function (result) {
            if (result) {
                $.ajax({
                    url: "@Url.Action("DeleteOrder", "Order")",
                    type: "POST",
                    data: { orderCode: orderCode },
                    success: function (result) {
                        if (result == "@XukaShop.Common.CommonConst.SUCCESSFUL") {
                            $('.close').click();
                            hideTableRow(promotionId);
                            showMessageDeleteSuccess();
                        }
                        if (result == "@XukaShop.Common.CommonConst.FAIL") {
                            showMessageDeleteFail();
                        }
                    }
                });
            }
        });
    }

    function hideTableRow(orderCode) {
        var rowId = "delete_order_" + orderCode;
        var tr = $("#" + rowId).closest('tr');
        tr.fadeOut(400, function () {
            tr.remove();
        });
        return false;
    }
</script>
